﻿using System;
using RAP.Framework.Libary.DomTemplateBuilder.Element.StringActions;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.StringActions
{
	/// <summary>
	/// 
	/// </summary>
	public class StringIsStartWithHandler : StringActionConditionalElementHandler
	{
		public new StringIsStartWith Element
		{
			get
			{
				return base.Element as StringIsStartWith;
			}
		}

		public override bool IsCondition ( object rootParamObject, object parentParamObject, ITemplateRenderContext context )
		{
			var exp = this.GetPropertyValue ( rootParamObject, parentParamObject, context ) as string;
			if ( string.IsNullOrEmpty ( exp ) )
			{
				return false;
			}
			var find = ExpressionUtils.GetFieldValue
			(
				this.Element.Find
				, rootParamObject
				, parentParamObject
				, context
				, ( ) => { return this.Element.Find; }
			) as string;

			var comparisonType = this.Element.ComparisonType;
			var finds = find.Split ( Consts.STRING_SPLIT_CHAR, StringSplitOptions.RemoveEmptyEntries );
			foreach ( var val in finds )
			{
				if ( exp.StartsWith ( val.Trim ( ), comparisonType ) )
				{
					return true;
				}
			}
			return false;
		}


	}
}
